package com.bdqn.wang.servlet;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bdqn.wang.pojo.Order;
import com.bdqn.wang.pojo.OrderAll;
import com.bdqn.wang.pojo.User;
import com.bdqn.wang.service.OrderAllService;
import com.bdqn.wang.service.OrderService;
import com.bdqn.wang.service.impl.OrderAllServiceImpl;
import com.bdqn.wang.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/order"})
public class OrderServlet extends HttpServlet {
    OrderService orderService = new OrderServiceImpl();
    OrderAllService orderAllService = new OrderAllServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        PrintWriter out = resp.getWriter();
        if ("my".equals(opr)){
//            List<Order> orderList = orderService.getAll();
//            req.getSession().setAttribute("list",orderList);
            req.getRequestDispatcher("/WEB-INF/order/order_my.jsp").forward(req,resp);
//            out.print(JSON.toJSONString(orderList));
        }else if("list".equals(opr)){
            List<Order> orderList = orderService.getAll();
            req.getSession().setAttribute("list",orderList);
//            String json = JSONArray.toJSONString(orderList);
            resp.getWriter().print(JSON.toJSONString(orderList));
        }else if("getId".equals(opr)){
            String id = req.getParameter("id");
            Order order = orderService.getOrderByid(Integer.parseInt(id));
            req.getSession().setAttribute("order",order);
            req.getRequestDispatcher("/WEB-INF/order/order_my_update.jsp").forward(req,resp);
        }else if("getUp".equals(opr)){
            Order order = new Order();
            order.setId(Integer.parseInt(req.getParameter("id")));
            order.setUserId(Integer.parseInt(req.getParameter("userId")));
            order.setLoginName(req.getParameter("loginName"));
            order.setUserAddress(req.getParameter("userAddress"));
            order.setCreateTime(DateUtil.parse(req.getParameter("createTime")));
            order.setCost(req.getParameter("cost"));
            order.setSerialNumber(req.getParameter("serialNumber"));
            int count = orderService.update(order);
            if(count>0){
                req.getRequestDispatcher("/WEB-INF/order/order_my.jsp").forward(req,resp);
            }else{
                resp.getWriter().print("<script>alert('修改')</script>");
            }
        }else if("del".equals(opr)){
            String id = req.getParameter("id");
            int count = orderService.del(Integer.parseInt(id));
            if(count>0){
//                resp.getWriter().print("<script>alert('删除成功')</script>");
                req.getRequestDispatcher("/WEB-INF/order/order_my.jsp").forward(req,resp);
            }else {
                resp.getWriter().print("<script>alert('删除失败')</script>");
            }
        }else if("detail".equals(opr)){
            String xiang = req.getParameter("xiang");
        }else if("all".equals(opr)){
            req.getRequestDispatcher("/WEB-INF/order/order_all.jsp").forward(req,resp);
        }else if("listAll".equals(opr)){
            List<OrderAll> orderAllList = orderAllService.getAll();
            req.getSession().setAttribute("listAll",orderAllList);
//            String json = JSONArray.toJSONString(orderList);
            resp.getWriter().print(JSON.toJSONString(orderAllList));
        }else if("Alldel".equals(opr)){
            String id = req.getParameter("id");
            int count = orderAllService.del(Integer.parseInt(id));
            if(count>0){
//                resp.getWriter().print("<script>alert('删除成功')</script>");
                req.getRequestDispatcher("/WEB-INF/order/order_all.jsp").forward(req,resp);
            }else {
                resp.getWriter().print("<script>alert('删除失败')</script>");
            }
        }
    }
}
